﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.WebUserControls.Frame
{
    public partial class TeamListFrame : System.Web.UI.UserControl
    {
        /// <summary>
        /// Gets or sets the department id.
        /// </summary>
        /// <value>
        /// The department id.
        /// </value>
        [Category("Manipulation")]
        public int DepartmentId
        {
            get { return ViewState["DepartmentId"] != null ? (int)ViewState["DepartmentId"] : -1; }
            set { ViewState["DepartmentId"] = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as TeamFilter ?? new TeamFilter { DepartmentId = DepartmentId };
            if (!Page.IsPostBack)
            {
                ViewState["filter"] = filter;
                ViewState["sortingButton"] = LinkButtonAll.ClientID;
            }
        }

        /// <summary>
        /// Handles the Selecting event of the ObjectDataSourceTeam control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSourceTeam_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as TeamFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the Command event of the FilterByLetters control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            var filter = ViewState["filter"] as TeamFilter;
            if (filter != null)
            {
                var beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);

                var letterGroupId = ViewState["sortingButton"] as string;
                var letterGroupButton = FindControl(letterGroupId) as LinkButton;
                if (letterGroupButton != null)
                {
                    var clickedButton = sender as LinkButton;
                    if (clickedButton == null)
                    {
                        return;
                    }

                    letterGroupButton.CssClass = string.Empty;
                    clickedButton.CssClass = "hg-blue";
                    ViewState["sortingButton"] = clickedButton.ClientID;
                }

                GridViewTeam.PageIndex = 0;
                GridViewTeam.DataBind();
            }
        }

        /// <summary>
        /// Checks the box inactive checked changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactiveCheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as TeamFilter;
            if (filter != null) filter.IncludeInactive = CheckBoxInactive.Checked;
            this.GridViewTeam.DataBind();
        }

        /// <summary>
        /// Handles the Command event of the LinkCreate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkCreate_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect("~/TeamMaintenance/TeamDetails.aspx?departmentId=" + DepartmentId);
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonTeamName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonTeamName_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                var teamId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                TeamBO.SetTeamStatus(teamId, true);
            }

            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/TeamMaintenance/TeamDetails.aspx?teamId={0}", e.CommandName));
        }
    }
}